import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta:{
        showTabbar:true,
      }
    },
    {
      path: '/forum',
      name: 'forum',
      component: () => import('../views/Forum.vue'),
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('../views/CartView.vue'),
      meta:{
        showTabbar:true,
        requireLogin:true,
      }
    },
    {
      path: '/my',
      name: 'my',
      component: () => import('../views/MyView.vue'),
      meta:{
        showTabbar:true,
        requireLogin:true,
      }
    },
    {
      path: '/class',
      name: 'class',
      component: () => import('../views/ClassView.vue'),
      meta:{
        showTabbar:true,
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
    },
    {
      path: '/reg',
      name: 'reg',
      component: () => import('../views/RegVew.vue'),
    },
    {
      path: '/item',
      name: 'item',
      component: () => import('../views/itemView.vue'),
    },
    {
      path: '/userInfo',
      name: 'userInfo',
      component: () => import('../views/userInfoView.vue'),
      meta:{
        showTabbar:true,
        requireLogin:true,
      }
    },
    {
      path: '/detail', 
      name: 'detail',
      component: () => import('../views/itemView.vue'),
    },
  ],
})


router.beforeEach((to,from)=>{
  if(to.meta.requireLogin == true){
    let token = localStorage.getItem("user-token")
    if(token ==  null){
      // showToast('请先登录');
      showConfirmDialog({
        title: '消息提示',
        message:
          '请先去登录,才能进行访问哦! ovo',
      })
        .then(() => {
          // on confirm
          router.push('/login')
        })
        .catch(() => {
          // on cancel
          router.push('/')
        });
      return false
    }
  }
})

export default router
